﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using YY.Common;
using YY.UI.BaseDataSvc;

namespace YY.UI
{
    public partial class RoleModuleFrm : BaseFrm
    {
        /// <summary>
        /// 菜单临时变量
        /// </summary>
        IEnumerable<TB_SysModule> _Menus = null;
        public RoleModuleFrm()
        {
            InitializeComponent();
            this.BaseDataProxy = new YY.UI.BaseDataSvc.BaseDataClient();
        }

        private void RoleModuleFrm_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy.Open();
            Common.BindEnumToComboBox(this.cbBoxRole, typeof(EnumCommon.SysRoleType), new string[] { EnumCommon.SysRoleType.管理员.ToString() }, EnumCommon.SysRoleType.客服员.ToString());

            this.lvLeft.View = View.Details;
            this.lvLeft.GridLines = true;
            this.lvLeft.FullRowSelect = true;//是否可以选择行
            this.lvLeft.MultiSelect = true;
            this.lvLeft.CheckBoxes = true;
            this.lvLeft.Columns.Add("", 30);
            this.lvLeft.Columns.Add("id", 0);
            this.lvLeft.Columns.Add("未选菜单", 200);

            this.lvRight.View = View.Details;
            this.lvRight.GridLines = true;
            this.lvRight.FullRowSelect = true;//是否可以选择行
            this.lvRight.MultiSelect = true;
            this.lvRight.CheckBoxes = true;
            this.lvRight.Columns.Add("", 30);
            this.lvRight.Columns.Add("id", 0);
            this.lvRight.Columns.Add("已选菜单", 200);

            bindData();
        }

        private void bindData()
        {
            this.lvLeft.Items.Clear();
            this.lvRight.Items.Clear();
            if (this.cbBoxRole.SelectedItem != null)
            {
                int ownerId = Convert.ToInt32(Enum.Parse(typeof(EnumCommon.SysRoleType), this.cbBoxRole.Text));
                IEnumerable<TB_RoleModule> roleModules = this.BaseDataProxy.FindRoleModules(ownerId);
                this._Menus = this.BaseDataProxy.FindSysModules(null, null);
                if (this._Menus != null)
                {
                    foreach (TB_SysModule m in this._Menus)
                    {
                        ListViewItem lvItem = new ListViewItem();
                        lvItem.Checked = true;
                        lvItem.SubItems.Add(m.id.ToString());
                        lvItem.SubItems.Add(m.moduleName);
                        if (roleModules != null && roleModules.Any(p => p.moduleId == m.id))
                            this.lvRight.Items.Add(lvItem);
                        else
                            this.lvLeft.Items.Add(lvItem);
                    }
                }
            }
        }

        private void btnMoveRight_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvItem in this.lvLeft.Items)
            {
                if (lvItem.Checked)
                {
                    ListViewItem nItem = new ListViewItem();
                    nItem.Checked = true;
                    nItem.SubItems.Add(lvItem.SubItems[1]);
                    nItem.SubItems.Add(lvItem.SubItems[2]);
                    this.lvRight.Items.Add(nItem);
                    this.lvLeft.Items.Remove(lvItem);
                }
            }
        }

        private void btnMoveLeft_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem lvItem in this.lvRight.Items)
            {
                if (lvItem.Checked)
                {
                    ListViewItem nItem = new ListViewItem();
                    nItem.Checked = true;
                    nItem.SubItems.Add(lvItem.SubItems[1]);
                    nItem.SubItems.Add(lvItem.SubItems[2]);
                    this.lvLeft.Items.Add(nItem);
                    this.lvRight.Items.Remove(lvItem);
                }
            }
        }

        bool leftItemChecked = false;
        private void lvLeft_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column == 0)
                setListViewItemChecked(this.lvLeft, ref leftItemChecked);
        }

        bool rightItemChecked = false;
        private void lvRight_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column == 0)
                setListViewItemChecked(this.lvRight, ref rightItemChecked);
        }

        private void setListViewItemChecked(ListView lv, ref bool itemChecked)
        {
            itemChecked = !itemChecked;
            foreach (ListViewItem lvItem in lv.Items)
                lvItem.Checked = itemChecked;
        }

        private void cbBoxRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindData();
        }

        private void tsBtnSave_Click(object sender, EventArgs e)
        {
            if (this.cbBoxRole.SelectedItem != null)
            {
                int ownerId = Convert.ToInt32(Enum.Parse(typeof(EnumCommon.SysRoleType), this.cbBoxRole.Text));
                
                List<TB_RoleModule> datas = null;
                if (this.lvRight.Items.Count > 0)
                {
                    datas = new List<TB_RoleModule>();
                    foreach (ListViewItem lvItem in this.lvRight.Items)
                    {
                        TB_RoleModule rm = new TB_RoleModule();
                        rm.roleId = ownerId;
                        rm.moduleId = Convert.ToInt32(lvItem.SubItems[1].Text);
                        datas.Add(rm);
                    }
                }
                if (this.BaseDataProxy.UpdateRoleModule(ownerId, datas.ToArray()))
                    MessageBox.Show("保存成功！");
                else
                    MessageBox.Show("保存异常！");
            }
        }

        private void tsBtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
